<?php

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			site_specific/ggw.php
#	FUNCTION:		Formatting the Magnetic Resonance Technology page output
#	AUTHOR:			Cameron Morrow
#	CREATED:		18/09/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# C A N   E D I T ?
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Compact?
$PAGE_COMPACT = (@$_GET["compact"]) ? true : false;

# See if editing
$edit_page = (@$_GET["edit"] == "true" && !$PAGE_PREVIEW) ? true : false;

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# S T A R T   H T M L
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en-AU\" xml:lang=\"en-AU\">\n";
echo "<!-- Created by Cameron Morrow for Zone 4 -->\n";
echo "<head>\n";
echo "\t<title>" . $PROJECT_TITLE . " - " . $PAGE_NAME . "</title>\n";
echo "\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n";
echo "\t<script language=\"javascript\" type=\"text/javascript\" src=\"../site_specific/assets/scripts/tct.js\"></script>\n";
echo "\t<style type=\"text/css\">\n";
echo "\t\t@import url(\"../site_specific/assets/styles/main.css\");\n";
echo "\t</style>\n";
echo "</head>\n";

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# B O D Y
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
echo "<body onload=\"parseImagePopups();\" class=\"" . ($PAGE_COMPACT ? "compact" : ($IS_SHOWING_LOGIN ? "login" : "")) . "\">\n";

# Start container
echo "<div id=\"container\">";

	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	# H E A D E R
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	echo "<div id=\"header\">";
	echo "<h1>" . $PROJECT_TITLE . " - " . $PAGE_NAME . "</h1>\n";
	echo "</div>";

	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	# N A V I G A T I O N
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	echo "<div id=\"nav\">\n";
	echo "<ul class=\"nav\">\n";

	# Top level pages
	$top_level_pages = $PAGES -> getChildrenOfPage("");
	
	# If navigation items found
	if (count($top_level_pages) > 0) {

		$counter = 0;

		# For each
		foreach ($top_level_pages -> getPageIDs() as $nav_page_id) {

			# Get page details
			$nav_page = $top_level_pages -> getPage($nav_page_id);

			# Get list of groups authorized to view this page
			$nav_page_authorized = explode(",", $nav_page -> getData("p_access"));
			
			# Get List of groups of users
			$nav_group_authorized= explode(",", $USER -> getGroup());

			# Ensure it can be viewed at current authorization
			$nav_page_authorized = (findArrayInArray($nav_page_authorized, $nav_group_authorized) !== false);

			# Ensure it's in the navigation
			$nav_page_in_navigation = ($nav_page -> getData("p_innavigation") == "1");

			# Get class for <li> item
			$item_class = ($nav_page -> getID() == $PAGES -> getPageAtDepth($PAGE_ID, 0)) ? array("active") : array();

			# Is first?
			if ($counter == 0) $item_class[] = "first";

			if ($nav_page_authorized && $nav_page_in_navigation) {

				$menu_item=explode("-",$nav_page -> getData("p_name"));
				# Open nav item
				echo "<li class=\"" . implode(" ", $item_class) . "\"><a href=\"../" . $nav_page -> getData("p_section") . "/" . $nav_page -> getID() . ".php\">" . $menu_item[0]. "</a></li>";

				$counter++;
			}
		}
	}

	# Close navigation
	echo "</ul>\n";
	echo "</div>";
	
	if($PAGE -> getData("p_id")!="siteglossary"){
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	# M A I N   C O N T E N T
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
			$sidebar_content = getRecord("SELECT p_id, p_content FROM " . $PROJECT_DB_TABLES["pages"] . " WHERE p_id = 'maps' OR p_id = 'must_see' OR p_id = 'must_do' OR p_id = 'plan_your_trip'", "p_id");
		
			$failure_content = "<p>Content missing!</p>";
		
			$content = array();
			$content["maps"] = $failure_content;
			$content["must_see"] = $failure_content;
			$content["must_do"] = $failure_content;
			$content["plan_your_trip"] = $failure_content;
		
			if ($sidebar_content) {
		
				foreach ($content as $key => $value) {
					if (isset($sidebar_content[$key]) && $sidebar_content[$key]["p_content"] != "") {
						$value = parseFlashLinks(parseFlashXML($sidebar_content[$key]["p_content"]));
						$index = strpos($value, "textcontainer");
						if ($index !== false) {
							$value = substr($value, strpos($value, ">", $index) + 1);
							$value = substr($value, 0, strpos($value, "</td>"));
							$value = preg_replace(
								array("/<[^>]*>/i"),
								array(""),
								$value);
		
							if (strlen($value) > 150) {
								$value = substr($value, 0, 147) . "...";
							}
							$value = "<p>" . $value . "</p><p><a href=\"../site/" . $key . ".php\">More &raquo;</a></p>";
						} else {
							$value = $failure_content;
						}
					}
					$content[$key] = $value;
					
				}
			}
			$sqlscript="select * from mrt_pages where p_parent="."\"".$PAGE_ID."\" AND p_status<3 order by p_sort ";
			$current_data=getRecord($sqlscript,"p_id");
			
			/*$tmp_cont=0;
			$tmp_script = "select * from mrt_pages where p_status<3 order by p_sort ";
			$tmp_data = getRecord($tmp_script);
			foreach($tmp_data as $tmp_value){
				
				$tmp_output = parseFlashLinks(parseFlashXML($tmp_value["p_content"]));
				
				$tmp_cont+=substr_count($tmp_output,"<!--[if !IE]>-->");
				
				if(substr_count($tmp_output,"<!--[if !IE]>-->")!=0){
					echo $tmp_value["p_id"]."<br/>";
				}
			}
			echo "number of if ie:".$tmp_cont;*/
			
			echo "<div id=\"maincontent\">\n";
			echo "<div id=\"maincontentleft\">\n";
			echo "<ul id=\"verticalmenu\">";
			foreach ($current_data as $current_page_id => $current_page) {
				//echo $current_page["p_name"];
			echo "<li><a href=\"../" .$current_page["p_section"] . "/" .$current_page["p_id"] .".php\">" . $current_page["p_name"] . "</a></li>";
			}
			echo "</ul>";
			echo "</div>";
			echo "</div>";
			$FLAG_TOP_LEVEL=false;
			  if(in_array($PAGE_ID,array("mres_7014","mres_7005","mres_7007","mres_7009","mres_7016","mres_7008","mres7100","mres_7001","mres_7002","mres_7003","mres_7004","mres_7006","mres_7013"))){
			  		$FLAG_TOP_LEVEL=true;
			  }
				# RIGHT
			  echo "<div id=\"maincontentright\" style=\"font-family:Arial;\">" .
				  contentHeader($PAGE_NAME,$PAGE_ID,$FLAG_TOP_LEVEL) .
				  startContentBox() .
				  "<div class=\"cw\">";
	  
				  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
				  # P A G E   C O N T E N T
				  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

				  # Primary Output
				  if ($edit_page && $USER -> getCanApprove()) {
	  
					  # Width?
					  if ($PAGE_COMPACT) {
						  $width = 670;
						  $height = 400;
					  } else {
						  $width = 520;
						  $height = 600;
					  }
	  
					  # Flash editor
					  $main_output = "<p>" . generatePageEditFlash("../assets/flash/editor3_02.swf?host=../&passed_width=" . $width . "&passed_height=" . $height . "&page=" . $PAGE_ID . "&type=page", $width, $height) . "</p>";
				  } else {
	  
					  if (!isset($main_output)) {
						  # Prep content
						  $main_output = parseFlashLinks(parseFlashXML($PAGE_CONTENT));
						  
					  }
				  }
	  
				  # ASK IF EDITING
				  if ($USER -> getCanApprove() && !$PAGE_COMPACT && !$PAGE_PREVIEW) {
					  echo "<div class=\"canedit\">";
					  if (!$edit_page) {
						  echo "<form name=\"edithispageform\" id=\"editthispageform\" action=\"" . $PAGE_ID . ".php\" method=\"get\">";
						  echo "<p>As an administrator you can <input type=\"hidden\" name=\"edit\" id=\"edit\" value=\"true\" />";
						  echo "<input type=\"submit\" value=\"Edit this page\" /></p>";
						  echo "</form>";
						  
					  } else {
						  echo "<form name=\"edithispageform\" id=\"editthispageform\" action=\"" . $PAGE_ID . ".php\" method=\"get\">";
						  echo "<p>Once you have finished making changes, click <input type=\"hidden\" name=\"edit\" id=\"edit\" value=\"\" />";
						  echo "<input type=\"submit\" value=\"Finished editing\" /></p>";
						  echo "</form>";
					  }
	  
					  if ($PAGE -> getData("p_custom_content") != "") {
						  echo "<p>Note: This page uses a custom content (<em>" . $PAGE -> getData("p_custom_content") . "</em>), so changes you make may not be reflected in the site proper.</p>";
					  }
					  echo "</div>";
				  }
	  
				  # MESSAGES
				  $messages_output = "";
				  if (count($MESSAGE_ARRAY) > 0) {
	  
					  $messages_output .= "<ul id=\"messages\" class=\"messages\">\n";
	  
					  foreach ($MESSAGE_ARRAY as $message) {
						  $messages_output .= "<li class=\"messagetype" . $message[1] . "\">" . $message[0] . "</li>";
					  }
	  
					  $messages_output .= "</ul>\n";
				  }
				  echo $messages_output;
				  
				  # Fix main output
				  $main_output = str_replace(
					  array("%0a", "%09"),
					  array("", ""),
					  $main_output
				  );
	  
				  # MAIN
				  echo $main_output;
	  
	  
				  # ERRORS
				  if (count($ERROR_ARRAY) > 0 && $SHOW_ERROR_FEEDBACK) {
	  
					  echo "<h2>Errors (" . count($ERROR_ARRAY) . ")</h2>\n";
					  echo "<ul id=\"errors\" class=\"messages\">";
	  
					  foreach ($ERROR_ARRAY as $error) {
						  echo "<li class=\"messagetype" . $error[1] . "\">" . $error[0] . "</li>";
					  }
	  
					  echo "</ul>\n";
				  }
	  
				  # SQL QUERIES
				  if (count($SQL_ARRAY) > 0 && $SHOW_SQL_FEEDBACK) {
	  
					  echo "<h2>SQL Queries (" . count($SQL_ARRAY) . ")</h2>\n";
					  echo "<ul id=\"sqlqueries\" class=\"messages\">";
	  
					  foreach ($SQL_ARRAY as $sql_query) {
						  echo "<li>" . $sql_query . "</li>";
					  }
	  
					  echo "</ul>\n";
				  }
	  
			  echo "</div>" .
				  endContentBox();
			  
			  
			  #previous page and next page
			  $sql_parent="SELECT p_parent FROM mrt_pages WHERE p_id = '".$PAGE_ID."' AND p_status<3";
			  $parent_data=getRecord($sql_parent);
			   $nextpage_pageid="";
			   $previouspage_pageid="";
			   $lastlayer_pageid="";
			  if($parent_data[0]["p_parent"]!=""){
				  $sql_nextpage="SELECT * FROM mrt_pages WHERE p_parent =\"".$parent_data[0]["p_parent"]."\" AND p_status<3 ORDER BY p_sort";
				  $nextpage_data=getRecord($sql_nextpage);
				  
				  $page_count=count($nextpage_data);
				  $current_page_index=0;
				  foreach($nextpage_data as $nextpage_key=>$nextpage_value){
						if($nextpage_value["p_id"]==$PAGE_ID){
							$current_page_index=$nextpage_key;
							break;
						}
				  }
				echo "current_page_index".$current_page_index."<br/>";
				echo "page_count:".$page_count."<br/>";
					
				  if($current_page_index==$page_count-1){
					  $nextpage_pageid="";
					  if($page_count>1){
						$previouspage_pageid=$nextpage_data[$current_page_index-1]["p_id"];
					  }
					  else{
						$previouspage_pageid="";  
					  }
					  if($nextpage_data[$current_page_index]["p_parent"]!=""){
						$lastlayer_pageid=$nextpage_data[$current_page_index]["p_parent"];
					  }
					  else{
						  $lastlayer_pageid="";
					  }
				  }
				  else if($current_page_index==0){
					 $previouspage_pageid="";
					  if($page_count>1){
						$nextpage_pageid=$nextpage_data[$current_page_index+1]["p_id"];
						echo "nextpage_data:".$nextpage_data[$current_page_index+1]["p_id"];
					  }
					  else{
						$nextpage_pageid="";  
					  }
					  if($nextpage_data[$current_page_index]["p_parent"]!=""){
						$lastlayer_pageid=$nextpage_data[$current_page_index]["p_parent"];
					  }
					  else{
						  $lastlayer_pageid="";
					  }
				  }
				  else{
					  if($page_count>2){
						$previouspage_pageid=$nextpage_data[$current_page_index-1]["p_id"];				 
						$nextpage_pageid=$nextpage_data[$current_page_index+1]["p_id"]; 
					  }
					  if($nextpage_data[$current_page_index]["p_parent"]!=""){
						$lastlayer_pageid=$nextpage_data[$current_page_index]["p_parent"];
					  }
					  else{
						  $lastlayer_pageid="";
					  }
				  }
			  }
			  
			  
			  echo "<table width=\"100%\">";
			  echo "<tr><td align=\"center\" style=\"width:33%\">";
			  if($previouspage_pageid!=""){
				  echo "<form name=\"actionform1\" id=\"actionform1\" action=\"" . $previouspage_pageid . ".php\" method=\"post\">";
				  echo "<input type=\"image\" name=\"submit1\" src=\"../assets/images/previous.png\">";
				  echo "</form>";
			  }
			  echo "</td>";
			  echo "<td align=\"center\" style=\"width:33%\">";
			  if($lastlayer_pageid!=""){
				  echo "<form name=\"actionform2\" id=\"actionform2\" action=\"" . $lastlayer_pageid . ".php\" method=\"post\">";
				  echo "<input type=\"image\" name=\"submit2\" src=\"../assets/images/up.png\">";
				  echo "</form>";
			  }
			  echo "</td>";
			  echo "<td align=\"center\" style=\"width:33%\">";
			  if($nextpage_pageid!=""){
				  echo "<form name=\"actionform3\" id=\"actionform3\" action=\"" . $nextpage_pageid . ".php\" method=\"post\">";
				  echo "<input type=\"image\" name=\"submit3\" src=\"../assets/images/next.png\">";
				  echo "</form>";
			  }
			  echo "</td></tr>";
			  echo "</table>";
		  
		  	echo "</div>";
		  # End main content
		  echo "</div>";
		  
	}
	# For glossary page


	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	# F O O T E R
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	echo "<div id=\"footer\">\n";
	echo "<p>Copyright &copy; Zone4, 2010 | \n";
	if ($USER -> isLoggedIn()) {
		echo "<a href=\"../admin/adminhome.php\">admin</a> | <a href=\"?logout=1\">logout</a> | Logged in as <strong>" . $USER -> getFirstName() . " " . $USER -> getLastName() . "</strong> (" . $USER -> getGroupName() . ")";
	} else {
		echo "<a href=\"../admin/adminhome.php\">admin</a>";
	}
	echo " | v" . $EDITOR_VERSION . "</p>\n";
	echo "<p><a href=\"http://validator.w3.org/check?uri=referer\">XHTML</a> | CSS</p>";
	echo "</div>\n";

# End container
echo "</div>";


echo "<div id=\"popup\" >";//style=\"display: none;background:#EFEFEF;color:#222222;border:solid;border-color:#C0C0C0;width:600px;height:400px;\"
echo "<div id=\"handle\" style=\"\">";
 echo "<form name=\"popupform\" id=\"popupform\" action=\"" . $PAGE_ID . ".php\" method=\"get\">";
 
 echo "</form>";
 echo "</div>";
echo "</div>";

/*echo "<script language=\"javascript\">";
echo "var theHandle = document.getElementById(\"handle\");";
echo "var theRoot   = document.getElementById(\"popup\");";
echo "Drag.init(theHandle, theRoot);";
echo "</script>";*/


# End
echo "</body>" .
	"</html>";
?>